package com.sdlogan.pbudget;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
/**
 * The activity where users can submit new data into the database
 * @author Sean
 *
 */
public class SubmitForm extends Activity {
	
	long success;
	Intent myIntent;
	MoneyTable mt;
	private String descrip;
	private String value;
	private String moneytype;
	private String category;
	private String occurence;
	private String type;
	/**
	 * Initialize the xml and set variables
	 */
	public void onCreate (Bundle savedInstanceState){
		
	super.onCreate(savedInstanceState);
    setContentView(R.layout.submitform);
    ArrayAdapter<CharSequence> adapter3;
    mt = new MoneyTable(this);
    Bundle extras = getIntent().getExtras();
    type = extras.getString("type");
    Log.i("Sean","Type Input" + type);
    final EditText et1 = (EditText) findViewById(R.id.descsubmit);
    final EditText et2 = (EditText) findViewById(R.id.valuesubmit);
    final Spinner occspin = (Spinner) findViewById(R.id.occspin);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.occurence_array, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    occspin.setAdapter(adapter2);
    final Spinner catspin = (Spinner) findViewById(R.id.catspin);
    if (type.equals("Expense")) {
    adapter3 = ArrayAdapter.createFromResource(this, R.array.cat_array, android.R.layout.simple_spinner_item);
    }
    else {
    adapter3 = ArrayAdapter.createFromResource(this, R.array.income_array, android.R.layout.simple_spinner_item);
    }
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    catspin.setAdapter(adapter3);
    Button b = (Button) findViewById(R.id.newsubmit);
    b.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			descrip = et1.getText().toString();
			value = et2.getText().toString();
			category = catspin.getSelectedItem().toString();
			occurence = occspin.getSelectedItem().toString();
			mt.open();
			success = mt.createNewRow(type, descrip, value,category,occurence);
			mt.close();
			if (success != -1) {
					Toast.makeText(getApplicationContext(), "Successfully Input", Toast.LENGTH_LONG).show();
				}
			else {
					Toast.makeText(getApplicationContext(), "Unsuccessful Input", Toast.LENGTH_LONG).show();
			}
			Intent newIntent = new Intent(SubmitForm.this,SubmitForm.class);
			newIntent.putExtra("type",type);
			startActivity(newIntent);
		}
	});
	}
	 /**
	  * Inflate the menu options
	  */
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
}
	
	/**
	 * Handle the items selected in the menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.home:
			Log.i("Sean","Pressed Home");
			myIntent = new Intent(SubmitForm.this, Main.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.graphs:
			return true;
		case R.id.incomechart:
			Log.i("Sean","income Graph");
			myIntent = new Intent(SubmitForm.this,Graphs.class);
			myIntent.putExtra("type","Income");
			startActivityForResult(myIntent,0);
			return true;
		case R.id.expensechart:
			Log.i("Sean","expense graph");
			myIntent = new Intent(SubmitForm.this,Graphs.class);
			myIntent.putExtra("type","Expense");
			startActivityForResult(myIntent,0);
			return true;
		case R.id.incomes:
			Log.i("Sean","Pressed Result");
			myIntent = new Intent(SubmitForm.this, Incomes.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.expenses:
			Log.i("Sean","Pressed Expense");
			myIntent = new Intent(SubmitForm.this,Expenses.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.export:
			Log.i("Sean","Pressed Export");
			myIntent = new Intent(SubmitForm.this, Export.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.newvalue:
			Log.i("Sean","Pressed New");
			return true;
		case R.id.incomenew:
			Log.i("Sean","Pressed Income");
			myIntent = new Intent(SubmitForm.this,SubmitForm.class);
			myIntent.putExtra("type","Income");
			startActivityForResult(myIntent,0);
			return true;
		case R.id.expensenew:
			Log.i("Sean","Pressed Expense");
			myIntent = new Intent(SubmitForm.this,SubmitForm.class);
			myIntent.putExtra("type","Expense");
			startActivityForResult(myIntent,0);
			return true;
		default: 
			return false;
		}
		
	}
	
}
